package com.example.meetdroid.main;

import java.util.ArrayList;
import java.util.List;

import com.example.meetdroid.Interfaces.IClientEvents;
import com.example.meetdroid.Interfaces.IPositionEvents;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class PositionWatcher implements LocationListener{
	 private LocationManager locationManager;
	 private String provider;
	 private Activity parentActivity;

	public PositionWatcher(Activity pRunInActivity){
		 try {
			 this.parentActivity = pRunInActivity;
		    // Get the location manager
			locationManager = (LocationManager) parentActivity.getSystemService(Context.LOCATION_SERVICE);
			// Define the criteria how to select the locatioin provider -> use
			// default
			Criteria criteria = new Criteria();
			criteria.setAccuracy(Criteria.ACCURACY_FINE);
			provider = locationManager.getBestProvider(criteria, false);
			if(provider != null){
				Location location = locationManager.getLastKnownLocation(provider);
		
				//Initialize the location fields
				if (location != null) {
					System.out.println("Provider " + provider + " has been selected.");
					onLocationChanged(location);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}	
	}
	
	public void requestLocationUpdates(long pTime,float pDistance){
		try {
			locationManager.requestLocationUpdates(provider, pTime, pDistance, this);
		} catch (Exception e) {
			Log.e("MeetUp", e.getMessage());
		}
		
	}
	public void removeUpdates(){
		locationManager.removeUpdates(this);
	}
	@Override
	
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		
		double lat =(double)Math.round(location.getLatitude() * 100000) / 100000;
		double lng =(double)Math.round(location.getLongitude() * 100000) / 100000;
		
		Globals.Client.SendGPSData(lat, lng);
		this.OnMyLocationChanged(location);
		this.OnMyLocationChanged(lat,lng);
	}


	@Override
	public void onProviderDisabled(String provider) {

		this.OnMyProviderDisabled(provider);
	}
	@Override
	public void onProviderEnabled(String provider) {

		this.OnMyProviderEnabled(provider);
	}
	
	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		
		this.OnMyStatusChanged(provider, status, extras);

	}
	
	List<IPositionEvents> listeners = new ArrayList<IPositionEvents>();
	public void addListener(IPositionEvents toAdd) {
        listeners.add(toAdd);
    }
	private void OnMyProviderEnabled(String provider){
        // Notify everybody that may be interested.
		for (IPositionEvents hl : listeners)
			hl.ProviderEnabled(provider);
	}
	private void OnMyProviderDisabled(String provider){
        // Notify everybody that may be interested.
		for (IPositionEvents hl : listeners)
			hl.ProviderEnabled(provider);
	}
	private void OnMyStatusChanged(String provider, int status, Bundle extras){
        // Notify everybody that may be interested.
		for (IPositionEvents hl : listeners)
			hl.StatusChanged(provider,status,extras);
	}
	private void OnMyLocationChanged(Location location){
        // Notify everybody that may be interested.
		for (IPositionEvents hl : listeners)
			hl.LocationChanged(location);
	}
	private void OnMyLocationChanged(double lat, double lng) {
		// TODO Auto-generated method stub
        // Notify everybody that may be interested.
		for (IPositionEvents hl : listeners)
			hl.LocationChanged(lat,lng);

	}
}

